﻿using UnityEngine;
using System.Collections;

public class JoggerBuilder : MonoBehaviour {

	//public Jogge
	public int _amountOfJoggersOnTeam;
	public int _amountOfTeams;
	public int _upSpeedBound;
	public int _lowerSpeedBound;
	public float _upperDistanceBound;
	public float _loverDistanceBound;
	public Level _level;

	public RuntimeAnimatorController _yourTeamRunAnimation;
	public RuntimeAnimatorController _yourTeamPantAnimation;
	public RuntimeAnimatorController _redTeamRunAnimation;
	public RuntimeAnimatorController _redTeamPantAnimation;
	public RuntimeAnimatorController _otherTeamRunAnimation;
	public RuntimeAnimatorController _otherTeamPantAnimation;




	private GameObject [] joggers;

	private int teamCount =0;
	private int joggerCount = 0;

	// Use this for initialization
	void Start () {
	
		joggers = new GameObject [_amountOfTeams * _amountOfJoggersOnTeam];


	}
	
	// Update is called once per frame
	void Update () {

		//for (int teamCount = 0; teamCount < _amountOfTeams; teamCount++) {
			
			//for (int joggerCount = 0; joggerCount < _amountOfJoggersOnTeam; joggerCount++) {

		if (teamCount < _amountOfTeams) {
				//Create new Prefab here
				GameObject newJogger = Instantiate(Resources.Load("jogger")) as GameObject;

				float xOffSet = Random.Range(_upperDistanceBound, _loverDistanceBound);
				newJogger.transform.position =  
					new Vector3(
					_level._levelStart.transform.position.x + xOffSet, 
						_level._levelStart.transform.position.y,
						_level._levelStart.transform.position.z
					);

				Jogger joggerScript = newJogger.GetComponent<Jogger>();
				int speed = Random.Range(_lowerSpeedBound, _upSpeedBound);
				joggerScript.setNaturalSpeed( (float) speed / 10000);
				joggerScript.setTeam(teamCount);
				
				//(Rigidbody) Instantiate(rocket, transform.position, transform.rotation);
				
				if (joggerScript.getTeam() == Jogger.YOUR_TEAM) {
				joggerScript._pantingSprite = _yourTeamPantAnimation;
				joggerScript._runningSprite = _yourTeamRunAnimation ;
			} else if (joggerScript.getTeam() == Jogger.RED_TEAM) {
				joggerScript._pantingSprite =_redTeamPantAnimation;
				joggerScript._runningSprite = _redTeamRunAnimation;
				} else {
					joggerScript._pantingSprite = _otherTeamPantAnimation;
					joggerScript._runningSprite = _otherTeamRunAnimation;
				}


				// custom 
				joggers[teamCount * _amountOfJoggersOnTeam + joggerCount] = newJogger;
		}


		joggerCount++;

		if (joggerCount >= _amountOfJoggersOnTeam) {
			teamCount++;
			joggerCount = 0;
		}

			//}
			
		//}


	}

	public GameObject [] getJoggers() {
		return joggers;
	}

}
